﻿<?php
// ------------------------------------------------------------------
// NEEDED STUBS FOR THIS EXAMPLE
// ------------------------------------------------------------------


// this will include all needed files to be able to build a Request
include_once($CONF["STUBS_FOLDER"]."Map24MatrixRouting/Map24MatrixRouting.php5");


// ------------------------------------------------------------------
// NEEDED PARAMETERS FOR THIS REQUEST
// ------------------------------------------------------------------


// the start address (coordinates)
$sLongitude = '521.0196';
$sLatitude 	= '3006.554';
$sRoadName  = 'Goethestr.';

// definition of the destinations

// first destination
$desIdx=0;
$Destinations[$desIdx]['Longitude']='521.0196';
$Destinations[$desIdx]['Latitude']='3007.554';
$Destinations[$desIdx]['RoadName']='';

// next destination
$desIdx++;
$Destinations[$desIdx]['Longitude']='522.0196';
$Destinations[$desIdx]['Latitude']='3007.554';
$Destinations[$desIdx]['RoadName']='';

// next destination
$desIdx++;
$Destinations[$desIdx]['Longitude']='522.0196';
$Destinations[$desIdx]['Latitude']='3006.554';
$Destinations[$desIdx]['RoadName']='';

// next destination
$desIdx++;
$Destinations[$desIdx]['Longitude']='522.0196';
$Destinations[$desIdx]['Latitude']='3005.554';
$Destinations[$desIdx]['RoadName']='';

// next destination
$desIdx++;
$Destinations[$desIdx]['Longitude']='521.0196';
$Destinations[$desIdx]['Latitude']='3005.554';
$Destinations[$desIdx]['RoadName']='';

// next destination
$desIdx++;
$Destinations[$desIdx]['Longitude']='520.0196';
$Destinations[$desIdx]['Latitude']='3005.554';
$Destinations[$desIdx]['RoadName']='';

// next destination
$desIdx++;
$Destinations[$desIdx]['Longitude']='520.0196';
$Destinations[$desIdx]['Latitude']='3006.554';
$Destinations[$desIdx]['RoadName']='';

// next destination
$desIdx++;
$Destinations[$desIdx]['Longitude']='520.0196';
$Destinations[$desIdx]['Latitude']='3007.554';
$Destinations[$desIdx]['RoadName']='';

// the language for the route description
$descriptionLanguage = "DE";		//EN

//show routes
$SynchronizeClient = "TRUE";


// ------------------------------------------------------------------
// BUILDING THE REQUEST
// ------------------------------------------------------------------


// definition of the request header with Map24ID and SessionID
$header = new RequestHeader();
$header->Map24ID  = $CONF['MAP24_ID'];
$header->ClientID = $CONF['SESSION_ID'];

// definition of the request body
$request = new CalculateMRoutesVectorRequest();


// define the Start Address by Coordinates
$request->Start = new MRoutePoint();
$request->Start->Coordinate = new Coordinate();
$request->Start->Coordinate->Longitude = $sLongitude;
$request->Start->Coordinate->Latitude = $sLatitude;
$request->Start->RoadName = $sRoadName;

//define the Destinations Addresses by Coordinates
for($desIdx=0;$desIdx<count($Destinations);$desIdx++)
{
	$DestinationsAry[$desIdx]= new MRoutePoint();
	$DestinationsAry[$desIdx]->Coordinate= new Coordinate();
	$DestinationsAry[$desIdx]->Coordinate->Longitude = $Destinations[$desIdx]['Longitude'];
	$DestinationsAry[$desIdx]->Coordinate->Latitude = $Destinations[$desIdx]['Latitude'];
	$DestinationsAry[$desIdx]->RoadName = $Destinations[$desIdx]['RoadName'];
}
if(count($DestinationsAry)>0)	{ $request->Destinations=$DestinationsAry; }

// define the Description Language
$request->DescriptionLanguage = $descriptionLanguage;

// define the Properties
if(is_array($property)) { $request->Properties = $property; }

//Show Routes
$request->SynchronizeClient= $SynchronizeClient;


// ------------------------------------------------------------------
// EXECUTE THE REQUEST
// ------------------------------------------------------------------


// in this object the SoapClient will be initiated and executed
$routing = new Map24MatrixRouting($CONF['MAPTP_SYSTEM'],$CONF['MAP24_ID'],$CONF['SESSION_ID']);
$response = $routing->calculateMRoutesVector($header,$request);


// ------------------------------------------------------------------
// OUTPUT THE RESULT
// ------------------------------------------------------------------


$resultString = "";
$counter = 0;

if( isset($response->Routes) )
{
	foreach ($response->Routes as $Route)
	{
		$counter++;
		$resultString .= "<div style='border:1px solid black;margin:2px 0px;padding:5px;'>".$counter.". ";
		$resultString .= "MRouteID: " . $Route->MRouteID . "<br /><br />";
		$resultString .= "TotalDistance: " . $Route->TotalDistance . " meter<br /><br />";
		$resultString .= "</div>";

		//We want to save the MRouteID of the second route in the session for a later example
		if($counter==2) { $RouteID=$Route->MRouteID; }
	}
	// save the MRouteID in a cookie (needed to show the route later)
	$t=date("U")+3600;
	SetCookie("MRouteID",$RouteID,$t);
}
else
{
	$resultString = "Sorry, no answer from the webservices";
}

?>